package com.tupu.controller.admin;

import com.tupu.common.PageResult;
import com.tupu.common.Result;
import com.tupu.entity.admin.RelationBuildingInfo;
import com.tupu.entity.admin.RelationInfo;
import com.tupu.service.admin.RelationBuildingService;
import com.tupu.service.admin.RelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/relation")
public class RelationController {

    @Autowired(required = false)
    private RelationBuildingService relationBuildingService;

    @Autowired(required = false)
    private RelationService relationService;

    /**
     * 添加关系
     *
     * @param relationInfo
     * @return
     */
    @PostMapping("/add")
    public Result addRelation(@RequestParam(required = false) String name,
                              @RequestParam(required = false) Long node_id,
                              RelationInfo relationInfo) {
        try {
            relationInfo.setName(name);
            relationInfo.setNodeId(node_id);
            relationService.save(relationInfo);
            return Result.ok("添加成功");
        } catch (Exception e) {
            return Result.errorMessage("新增关系失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据id删除关系,同时删除关系构建表，删除节点存在的关系
     *
     * @param id
     * @return
     */
    @PostMapping("/del")
    public Result del(@RequestParam(required = false) Integer id,
                      @RequestParam(required = false) Long node_id) {
        try {
            relationService.delRealtion(node_id, id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage("未删除关系,错误信息" + e.getMessage());
        }
    }

    /**
     * 根据id更新关系,修改实例关系名
     *
     * @param id
     * @param name
     * @param node_id
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestParam(required = false) Long id,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) Long node_id) {
        try {
            relationService.updateRelation(id, name, node_id);
            return Result.ok("修改成功");
        } catch (Exception e) {
            return Result.errorMessage("修改失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据node_id查询关系,无参数就查询所有关系列表
     *
     * @return
     */
    @PostMapping("/list")
    public Result<List<RelationInfo>> findAllRelation(@RequestParam(required = false) Long node_id) {
        List<RelationInfo> list;
        try {
            list = relationService.findRelByNodeId(node_id);
            if (null == list) {
                list = relationService.list();
            }
            return Result.ok(list);
        } catch (Exception e) {
            list = relationService.list();
            return Result.ok(list);
        }
    }

    /**
     * 构建 本体标签间关系
     *
     * @param start_id
     * @param end_id
     * @param relation_id
     * @param relation
     * @param node_id
     * @return
     */
    @PostMapping("/building")
    public Result addRelation(@RequestParam(required = false) Long start_id,
                              @RequestParam(required = false) Long end_id,
                              @RequestParam(required = false) Long relation_id,
                              @RequestParam(required = false) Long node_id,
                              @RequestParam(required = false) String relation) {
        try {
            String result = relationService.buildingRelation(start_id, end_id, relation_id, node_id, relation);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.errorMessage("添加失败:" + e.getMessage());
        }
    }

    /**
     * 通过关系id（relation_id） 查询关系数据 分页
     *
     * @param ids
     * @return
     */
    @PostMapping("/findById")
    public Result findList(@RequestParam(required = false, defaultValue = "1") Integer page,
                           @RequestParam(required = false, defaultValue = "10") Integer size,
                           @RequestParam(required = false, value = "id") List<Long> ids) {
        try {
            PageResult<RelationBuildingInfo> info = relationService.pageInfo(page, size, ids);
            return Result.ok(info);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 根据id 删除已构建的本体关系 关联表
     *
     * @param id
     * @return
     */
    @PostMapping("/delRelation")
    public Result delRelation(@RequestParam(required = false) Long id) {
        try {
            relationBuildingService.removeById(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage("删除失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据id编辑修改 本体间关系
     *
     * @param id
     * @param start_id
     * @param end_id
     * @param relation_id
     * @param relation
     * @param node_id
     * @return
     */
    @PostMapping("/updateRelation")
    public Result updateRelation(@RequestParam(required = false) Long id,
                                 @RequestParam(required = false) Long start_id,
                                 @RequestParam(required = false) Long end_id,
                                 @RequestParam(required = false) Long relation_id,
                                 @RequestParam(required = false) String relation,
                                 @RequestParam(required = false) Long node_id) {
        try {
            String result = relationService.updateRelationBuilding(id, start_id, end_id, relation_id, relation, node_id);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.errorMessage("编辑失败，错误信息：" + e.getMessage());
        }
    }

    /**
     * 根据关联表id查询一条关系列表
     *
     * @param id
     * @return
     */
    @PostMapping("/findOne")
    public Result<RelationBuildingInfo> findOne(@RequestParam(required = false) Long id) {
        try {
            RelationBuildingInfo rb = relationService.findOne(id);
            return Result.ok(rb);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 根据id 删除已构建的本体关系
     *
     * @param ids
     * @return
     */
    @PostMapping("/delByIds")
    public Result delRelationByIds(@RequestParam(required = false) List<Long> ids) {
        try {
            relationBuildingService.removeByIds(ids);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败,请重试,错误信息:" + e.getMessage());
        }
    }
}